package com.xiaoshuidi.cloud.framework.jackson.core.databind;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

/**
 * LocalDateTime反序列化规则
 * <p>
 * 会将毫秒级时间戳反序列化为LocalDateTime
 */
public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {

    public static final LocalDateDeserializer INSTANCE = new LocalDateDeserializer();

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        long mill = p.getValueAsLong(-1);
        if (mill > 0) {
            return Instant.ofEpochMilli(mill).atZone(ZoneId.systemDefault()).toLocalDate();
        }
        return com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.INSTANCE.deserialize(p, ctxt);
    }
}
